package com.css.fxfzaqbz.modules.assess.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.common.utils.excel.ExportUtil;
import com.css.fxfzaqbz.constants.FxfzConstants;
import com.css.fxfzaqbz.modules.assess.entity.DisasterAssEntity;
import com.css.fxfzaqbz.modules.assess.service.ResultAnalysisService;
import com.css.fxfzaqbz.modules.base.entity.HouseDestoryEntity;
import com.css.fxfzaqbz.modules.base.service.CountProcessService;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 评估结果分析-房屋地震灾害风险评估
 */
@RestController
@RequestMapping("/data/resultAnalysis")
@Slf4j
public class ResultAnalysisController {
    @Resource
    private ResultAnalysisService resultAnalysisService;
    @Resource
    private CountProcessService countProcessService;
    //评估结果分析-获取评估结果 参数活动id
    @GetMapping("/getResult")
    public RestResponse getResult(HttpServletRequest request,String id){
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> result = resultAnalysisService.getResult(curPage, pageSize, id);
            return RestResponse.succeed(result);
        } catch (Exception e) {
            log.error("获取失败",e);
            return RestResponse.fail("获取失败");
        }
    }
    //评估结果分析分页
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request,String id){
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = countProcessService.getPage(curPage,pageSize,id);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("获取失败",e);
            return RestResponse.fail("获取失败");
        }
    }
   //严重破坏和毁坏风险占比统计statistics
    @GetMapping("/statistics")
    public RestResponse statistics(String id){
        try {
            Map<String,String> map = countProcessService.statistics(id);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("获取失败",e);
            return RestResponse.fail("获取失败");
        }
    }
    //房屋数据导出
    @PostMapping("/exportExcel/{id}")
    public void exportExcel(@PathVariable String id, HttpServletResponse response){
        try {
            List<HouseDestoryEntity> list = countProcessService.exportExcel(id);
            for (HouseDestoryEntity entity : list) {
                entity.setJglx(castType(entity.getJglx()));
                entity.setMaxDestoryType(destroy(entity.getMaxDestoryType()));
            }
            ExportUtil.createExcel(response,list,"房屋数据评估结果",HouseDestoryEntity.class);
        } catch (Exception e) {
            log.error("房屋数据导出失败",e);
        }
    }

    private String castType(String type) {
        if (FxfzConstants.TUMU_STRUCTURE.equals(type)) {
            type = "土木/石木结构";
        }
        if (FxfzConstants.QITI_STRUCTURE.equals(type)) {
            type = "砌体结构";
        }
        if (FxfzConstants.MU_STRUCTURE.equals(type)) {
            type = "木结构";
        }
        if (FxfzConstants.YAOT_STRUCTURE.equals(type)) {
            type = "窑洞";
        }
        if (FxfzConstants.GJHNT_STRUCTURE.equals(type)) {
            type = "钢筋混凝土结构";
        }
        if (FxfzConstants.MUZHU_STRUCTURE.equals(type)) {
            type = "木（竹）结构";
        }
        if (FxfzConstants.HZ_STRUCTURE.equals(type)) {
            type = "混杂结构";
        }
        if (FxfzConstants.GANG_STRUCTURE.equals(type)) {
            type = "钢结构";
        }
        if (FxfzConstants.OTHER_STRUCTURE.equals(type)) {
            type = "其他结构";
        }
        if (FxfzConstants.KZQQT_STRUCTURE.equals(type)) {
            type = "底部框架-抗震墙砌体结构";
        }
        return type;
    }
    private String destroy(String type) {
        if ("0".equals(type)) {
            type = "基本完好";
        }
        if ("1".equals(type)) {
            type = "轻微破坏";
        }
        if ("2".equals(type)) {
            type = "中等破坏";
        }
        if ("3".equals(type)) {
            type = "严重破坏";
        }
        if ("4".equals(type)) {
            type = "毁坏";
        }
        return type;
    }
}
